package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.NearUserVo;
import com.itheima.service.mongo.UserLocationService;
import com.itheimma.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class UserLocationManager {

    @DubboReference
    private UserLocationService userLocationService;

    @DubboReference
    private UserInfoService userInfoService;

    //上报位置信息
    public void uploadLocation(String longitude, String latitude , String addrStr ){
        Long userId = UserHolder.get().getId();
        userLocationService.uploadLocation(userId, longitude, latitude, addrStr);
    }

    //搜附近
    public ResponseEntity searchNearUserId(String gender,Double distance ){
        Long userId = UserHolder.get().getId();

        List<Long> userIdList = userLocationService.searchNearUserId(userId, distance);
        ArrayList<NearUserVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userIdList)) {
            for (Long userID : userIdList) {
                NearUserVo nearUserVo = new NearUserVo();
                //TODO 判断是否为自己，是则跳出
                if (userID.equals(userId)) {
                    continue;
                }

                UserInfo userInfo = userInfoService.findById(userID);
                //判断是否为异性
                if (!StrUtil.equals(userInfo.getGender(), gender)) {
                    continue;
                }
                //封装vo
                nearUserVo.setUserId(userID);
                nearUserVo.setAvatar(userInfo.getAvatar());
                nearUserVo.setNickname(userInfo.getNickname());

                //封装集合
                vos.add(nearUserVo);
            }
        }
        return ResponseEntity.ok(vos);
    }
}
